package com.caimiao.miao.interfaces;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.caimiao.miao.R;
import com.caimiao.miao.library.utils.tip.LogUtil;

public abstract class BaseFragmentImpl<P extends IBasePresenter> extends Fragment implements IBaseFragment, IBaseView<P> {
    public View mRootView;
    protected P mPresenter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (getLayoutRes() != 0) mRootView = inflater.inflate(getLayoutRes(), null);
        initUI();
        initLogic();
        initCommon();
        return mRootView;
    }

    @Override
    public void onDestroy() {
        if (mPresenter != null) {
            mPresenter.detach();
            mPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    public void setPresenter(P presenter) {
        mPresenter = presenter;
        mPresenter.start(getContext());
    }


    public void initCommon() {
        View mBtnBack = mRootView.findViewById(R.id.navIBtnBack);
        if (mBtnBack != null) mBtnBack.setOnClickListener(this::onLeftClick);
    }

    @Override
    public void setTitle(int res) {
        setTitle(getString(res));
    }

    @Override
    public void setTitle(String title) {
        TextView mTvTitle = mRootView.findViewById(R.id.navTvTitle);
        if (mTvTitle != null)
            mTvTitle.setText(title);
    }

    @Override
    public void onLeftClick(View view) {
        LogUtil.e("BaseFragment", "返回被点击了");
    }

    @Override
    public void onRightClick(View view) {

    }

    @Override
    public void onRestart() {
    }
}
